package com.example.experiment5.controller;

import com.example.experiment5.entity.Admin;
import com.example.experiment5.entity.User;
import com.example.experiment5.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@org.springframework.stereotype.Controller
public class Controller {

    @Autowired
    private UserService service;

    @ResponseBody
    @GetMapping("/login")
    public List<Integer> login(@Param("username") String username, @Param("password") String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        List<User> us = service.UserLogin(user);
        List<Integer> list = new ArrayList<>();
        if(us.size()>0){
            list.add(1);
            list.add(us.get(0).getUserId());
        }else{
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            List<Admin> as = service.AdminLogin(admin);
            if(as.size()>0){
                list.add(2);
            }
        }
        return list;
    }

    @ResponseBody
    @GetMapping("/findUser/{userId}")
    public List<User> findStudent(@PathVariable("userId") String userId){
        List<User> list = service.selectUser(Integer.valueOf(userId));
        System.out.println(list);
        return list;
    }

    @ResponseBody
    @GetMapping("/findAllUser/{page}/{size}")
    public List<User> findAllUser(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        List<User> list = service.selectAllUser();
        System.out.println(list);
        int num = list.size();
        if(list.size()>=(page*size)){
            list = list.subList((page-1)*size, page*size);
        }else{
            list = list.subList((page-1)*size, list.size());
        }
        if(list.size()>0){
            list.get(0).setNum(num);
        }
        return list;
    }

    @ResponseBody
    @GetMapping("/clockedRecord/{page}/{size}/{userId}")
    public List<User> clockedRecord(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("userId") Integer userId){
        List<User> list = service.selectRecord(userId);
        System.out.println(list);
        int num = list.size();
        if(list.size()>=(page*size)){
            list = list.subList((page-1)*size, page*size);
        }else{
            list = list.subList((page-1)*size, list.size());
        }
        if(list.size()>0){
            list.get(0).setNum(num);
        }
        return list;
    }

    @ResponseBody
    @GetMapping("/clockedState/{clockId}")
    public int clockedState(@PathVariable("clockId") Integer clockId){
        User user = service.selectClocked(clockId);
        return user.getState();
    }

    @ResponseBody
    @GetMapping("/selectClocked/{clockId}")
    public User selectClocked(@PathVariable("clockId") Integer clockId){
        User user = service.selectClocked(clockId);
        return user;
    }

    @ResponseBody
    @GetMapping("/selectHaveClocked/{page}/{size}")
    public List<User> selectHaveClocked(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        List<User> list = service.selectHaveClocked();
        int num = list.size();
        if(list.size()>=(page*size)){
            list = list.subList((page-1)*size, page*size);
        }else{
            list = list.subList((page-1)*size, list.size());
        }
        if(list.size()>0){
            list.get(0).setNum(num);
        }
        return list;
    }

    @ResponseBody
    @GetMapping("/selectNotClocked/{page}/{size}")
    public List<User> selectNotClocked(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        List<User> list = service.selectNotClocked();
        System.out.println(list);
        List<User> users = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            List<User> s = service.selectLast(list.get(i).getUserId());
            users.add(s.get(0));
            users.get(i).setNum(s.size());
        }
        if(users.size()>=(page*size)){
            users = users.subList((page-1)*size, page*size);
        }else{
            users = users.subList((page-1)*size, users.size());
        }
        return users;
    }

    @ResponseBody
    @GetMapping("/findNotClockedNum")
    public Integer findNotClockedNum(){
        List<User> list = service.selectNotClocked();
        return list.size();
    }

    @ResponseBody
    @PostMapping("/insertClocked")
    public int insertClocked(@RequestBody User user){
        int i = service.insertClocked(user);
        return i;
    }

    @ResponseBody
    @GetMapping("/addClocked")
    public String addClocked(){
        List<User> list = service.selectAllUser();
        int num = 0;
        for(int i=0;i<list.size();i++){
            num+=service.addClocked(list.get(i).getUserId());
        }
        if(num==list.size()){
            return "success";
        }else{
            return "fail";
        }
    }

    @ResponseBody
    @PostMapping("/save")
    public String save(@RequestBody User user){
        int i = service.insertUser(user);
        if(i==1){
            return "success";
        }
        return "fail";
    }

    @ResponseBody
    @PostMapping("/updateClocked")
    public int updateClocked(@RequestBody User user){
        int i = service.updateClocked(user);
        return i;
    }

    @ResponseBody
    @PostMapping("/updateUser")
    public String updateUser(@RequestBody User user){
        System.out.println(user);
        int i = service.updateUser(user);
        if(i==1){
            return "success";
        }else{
            return "fail";
        }
    }

    @ResponseBody
    @GetMapping("/deleteUser/{userId}")
    public String deleteStudent(@PathVariable("userId") Integer userId){
        int i = service.deleteUser(userId);
        if(i==1){
            return "success";
        }else{
            return "fail";
        }
    }
}
